﻿using System;
using System.Data.Common;
using System.Data.OleDb;

namespace CommonUtils
{
    /// <summary>
    /// ADO.NET for OleDb
    /// </summary>
    public class DbOle : DbBase
    {
        /// <summary>
        /// 连接字符串
        /// 有时候，07可以兼容03,64可以兼容32
        /// 没有安装office可以安装AccessDatabaseEngine(microsoft office access database engine)
        /// https://www.microsoft.com/zh-cn/download/confirmation.aspx?id=13255
        /// https://www.microsoft.com/zh-cn/download/details.aspx?id=54920
        /// </summary>
        public class ConnectStrings
        {
            //必须32位模式启动
            public static string Access03(string path)
            => string.Format("Provider=Microsoft.JET.OLEDB.4.0;Data Source='{0}';", path);

            //必须32位模式启动
            public static string Excel03ReadOnly(string path)
            => string.Format("Provider=Microsoft.JET.OLEDB.4.0;Data Source='{0}';Extended Properties='Excel 8.0;HDR=YES;IMEX=1;';", path);

            //必须32位模式启动
            public static string Excel03ReadWrite(string path)
            => string.Format("Provider=Microsoft.JET.OLEDB.4.0;Data Source='{0}';Extended Properties='Excel 8.0;HDR=YES;IMEX=0;';", path);

            /// <summary>
            /// Paradox引擎，传奇，征途等
            /// 必须32位模式启动,否则 System.InvalidOperationException:“The 'Microsoft.JET.OLEDB.4.0' provider is not registered on the local machine.”
            /// </summary>
            public static string Paradox(string folder)
            => string.Format("Provider=Microsoft.JET.OLEDB.4.0;Data Source={0};Extended Properties=Paradox 5.x;", folder);

            public static string Access07(string path)
            => string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='{0}';", path);

            public static string Excel07ReadOnly(string path)
            => string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='{0}';Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';", path);

            public static string Excel07ReadWrite(string path)
            => string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='{0}';Extended Properties='Excel 12.0;HDR=YES;IMEX=0;';", path);

            //System.Data.OleDb.OleDbException:“Microsoft Access 不支持对此 IISAM 类型执行此操作。若要执行此操作，您必须使用 2007 Microsoft Office system 或更早版本。”
            //public static string Paradox07(string folder)
            //=> string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Paradox 5.x;", folder);
        }

        public DbOle(string connectString)
        : base(connectString) { }

        protected override DbConnection GetConnection()
        => new OleDbConnection(ConnectString);

        protected override DbParameter GetParameter(string key, object value)
        => new OleDbParameter(key, value);

        [Obsolete]
        protected override DbDataAdapter GetDataAdapter(DbCommand cmd)
        => new OleDbDataAdapter((OleDbCommand)cmd);
    }
}
